/**
 *
 * @constructor
 */
function showTips(options) {
    if(!options.hasOwnProperty("type")){
        options.type = "success";
    }
    if ($(document).tips != null) {
        $(document).tips(options)
    }
    if ($(document).tip != null) {
        $(document).tip(options)
    } else {
        console.log("no tips plugin " + options);
    }
}
function AjaxQueue() {
    var entity = this;
    var taskArr = [];
    var batchArr = [];
    var timerId = -1;
    var errorCollectArr = [];
    var afterCompleteArr = [];
    //-2:not start,-1,0,1:running,2:complete
    this.status = -2;//

    var checkComplete = function () {
        switch (entity.status) {
            case 1:
                clearInterval(timerId);
                timerId = -1
                if (errorCollectArr.length > 0) {
                    var totalError = errorCollectArr.join("\n");
                    errorCollectArr = [];
                    showTips({
                        content: totalError,
                        type: "error"
                    })
                }
                entity.status = 2;
                loadComplete();
                batchComplete();
                break;
            case -1:
                $.each(taskArr, function (index, obj) {
                    if (obj.responseData == null) {
                        var ajaxData = obj.ajaxData;
                        $.ajax({
                            type: ajaxData.type,
                            url: ajaxData.url,
                            dataType: ajaxData.dataType,
                            beforeSend: function (request) {
                                request.setRequestHeader("Sign", "#f93Uc31K24()_@");
                            },
                            data: ajaxData.data,
                            success: function (data) {
                                var tempTarget = findTaskByRegName(obj.regName);
                                if (tempTarget.needRemove == true) {
                                    entity.removeTask(obj.regName);
                                } else {
                                    tempTarget.responseData = data;
                                }
                                if (ajaxData.success != null) {
                                    ajaxData.success(data);
                                }
                            },
                            error: function (data) {
                                if (ajaxData.error != null) {
                                    ajaxData.error(data);
                                }
                                errorCollectArr.push(obj.regName + " 请求失败")
                                entity.removeTask(obj.regName)
                            }
                        });
                    }
                })
                entity.status = 0;
                timerId = setInterval(checkComplete, 160);
                break;
            case 0:
                var allComplete = true;
                $.each(taskArr, function (index, obj) {
                    if (obj.responseData == null) {
                        allComplete = false;
                        return true;
                    }
                });
                if (allComplete) {
                    entity.status = 1;
                }
                break;
        }
    }
    function findTaskByRegName(regName) {
        if (taskArr.length == 0) {
            return null;
        }
        var returnTarget = null;
        $.each(taskArr, function (index, obj) {
            if (obj.regName == regName) {
                returnTarget = obj;
                return false;
            }
        })
        return returnTarget;
    }

    this.push = function (regName, ajaxData, responseData, needRemove) {
        if(typeof(needRemove) != "boolean" ){
            needRemove = needRemove == null ? false : true
        }
        taskArr.push({"regName": regName, "ajaxData": ajaxData, "responseData": responseData, "needRemove": needRemove})
    }
    this.batchRequest = function (regName, ajaxData, sendDataArr, handler) {
        var acceptMsgArr = [];
        var batchRequestData = {"originalArr": sendDataArr, "acceptMsgArr": acceptMsgArr, handler: handler }
        batchArr.push(batchRequestData)
        $.each(sendDataArr, function (index, obj) {
            taskArr.push(
                {
                    "regName": regName + "_" + index,
                    "ajaxData": {
                        type: ajaxData.type,
                        url: ajaxData.url,
                        dataType: ajaxData.dataType,
                        data: obj,
                        success: function (data) {
                            var formatData = null;
                            if( data != null){
                                formatData = {"nodeIndex": index, "operationSuccess": false, "responseData": data, "msg": data.msg};
                                if (data.code == "A00000") {
                                    formatData.operationSuccess = true;
                                }
                            }else{
                                formatData = {"nodeIndex": index, "error": true, "operationSuccess": false, "responseData": data};
                            }
                            acceptMsgArr.push(formatData)
                        },
                        error: function (data) {
                            var formatData = {"nodeIndex": index, "error": true, "operationSuccess": false, "responseData": data};
                            acceptMsgArr.push(formatData);
                        }
                    },
                    "needRemove": true
                });
        })
    }

    this.startLoad = function () {
        showTips({
            content: "正在加载数据"
        })
        if (entity.status != -2 && entity.status != 2) {
            return false;
        }
        entity.status = -1;
        checkComplete();
        return true;
    }
    this.findResponseData = function (regName) {
        var task  = findTaskByRegName(regName);
        if(task == null){
            return null;
        }
        return  task.responseData;
    }
    this.clearTaskResponse = function (regName) {
        var task = findTaskByRegName(regName);
        if (task != null) {
            task.responseData = null
        }
    }
    this.removeTask = function (regName) {
        var task = findTaskByRegName(regName);
        if (task != null) {
            var taskIndex = taskArr.indexOf(task)
            taskArr.splice(taskIndex, 1);
        }
    }

    var batchComplete = function () {
        while (batchArr.length > 0) {
            var batchRequestData = batchArr.pop();
            batchRequestData.handler({"originalArr": batchRequestData.originalArr,
                    "acceptArr": batchRequestData.acceptMsgArr}
            );
        }
    }

    var loadComplete = function () {
        console.log("loadComplete over");
        while(afterCompleteArr.length > 0){
            var excuteFunc = afterCompleteArr.shift();
            excuteFunc.call();
        }
    }

    this.afterLoadComplete = function( callback ){
        if( typeof(callback) == "function"){
            afterCompleteArr.push(callback );
        }
    }

    this.isRunning = function(){
        return this.status == 0;
    }

}